<template>
  {{ id }}
  <el-form :model="form" label-width="auto" style="max-width: 600px">
    <el-form-item label="编号">
      <el-input v-model="form.id" disabled/>
    </el-form-item>
    <el-form-item label="姓名">
      <el-input v-model="form.name"/>
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="form.password"/>
    </el-form-item>
    <el-form-item label="年级">
      <el-select v-model="form.classInfo.name">
        <el-option v-for="item in classInfoList" :value="item.id">{{ item.name }}</el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="出生日期">
      <el-col :span="11">
        <el-date-picker
            v-model="form.birthday"
            type="date"
            placeholder="Pick a date"
            style="width: 100%"
        />
      </el-col>
    </el-form-item>
    <el-form-item label="年龄">
      <el-input v-model="form.age"/>
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="form.gender">
        <el-radio value="男">男</el-radio>
        <el-radio value="女">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">Create</el-button>
      <el-button>Cancel</el-button>
    </el-form-item>
  </el-form>

</template>
<script>
import axios from "axios";

export default {
  name: "UserUpdate",
  props: ["id"],
  data() {
    return {
      form: {
        "id": 1,
        "name": "张三",
        "password": "123",
        "gender": "男",
        "birthday": "2000年02月02日",
        "age": 24,
        "classInfo": {
          "id": 1,
          "name": "一年级"
        }
      }
      , classInfoList: []
    }
  }
  , methods: {
    onSubmit() {

    }
    , findById(id) {
      // console.log(this.id)
      // 根据id查询用户
      axios.get(`http://localhost:8080/user2/${id}`)
          .then(result=>{
            // console.log(result)
            this.form = result.data
          })
    }
    , findClassInfo() {
      axios.get("http://localhost:8080/class/findAll")
          .then(result => {
            console.log(result.data)
            this.classInfoList = result.data
          })
    }
  }
  // watch  vue的监听属性 可以监听某一个值的变化 参数是 新值,旧值
  ,watch:{
    id(newVal,oldVal){
      console.log(oldVal,newVal)
      this.findById(newVal)
    }
  }
  , mounted() {
    // 根据id查询 用户
    this.findById(this.id)
    this.findClassInfo()
  }

}
</script>


<style scoped>
.el-form {
  margin: 20px;
}
</style>